﻿using System;
using System.Collections.Generic;

namespace h3
{
    public delegate void GradePrint(Student s);

    public class Student
    {
        public string Name { get; set; }
        public int Id { get; set; }
        public Dictionary<string, double> Grades { get; set; }
        public GradePrint GP { get; set; }

        public Student(int id, string name)
        {
            Id = id;
            Name = name;
            Grades = new Dictionary<string, double>();
        }

        public void AddGrade(string term, double score)
        {
            if (!Grades.ContainsKey(term))
            {
                Grades.Add(term, score);
            }
            else
            {
                Grades[term] = score;
            }
        }

        public void PrintGrade()
        {
            if (GP != null)
            {
                GP(this);
            }
            else
            {
                Console.WriteLine("未设置成绩打印模板");
            }
        }
    }

    public class GradeReport
    {
        public static void OrderByTerm(Student s)
        {
            Console.WriteLine($"=== {s.Name}({s.Id})的成绩报告（按学期） ===");
            foreach (var grade in s.Grades)
            {
                Console.WriteLine($"{grade.Key}: {grade.Value}分");
            }
            Console.WriteLine("==========================================");
        }

        public static void OrderByScore(Student s)
        {
            Console.WriteLine($"=== {s.Name}({s.Id})的成绩报告（按成绩高低） ===");
            var sortedGrades = new SortedDictionary<double, string>(Comparer<double>.Create((x, y) => y.CompareTo(x)));
            foreach (var grade in s.Grades)
            {
                sortedGrades.Add(grade.Value, grade.Key);
            }
            foreach (var grade in sortedGrades)
            {
                Console.WriteLine($"{grade.Value}: {grade.Key}分");
            }
            Console.WriteLine("==========================================");
        }

        public static void SimpleFormat(Student s)
        {
            Console.WriteLine($"{s.Name}的成绩：");
            double total = 0;
            foreach (var grade in s.Grades)
            {
                total += grade.Value;
                Console.Write($"{grade.Key}:{grade.Value}  ");
            }
            Console.WriteLine($"\n平均分：{total / s.Grades.Count:F2}");
            Console.WriteLine("------------------------------------------");
        }
    }

    internal class Program
    {
        public static void Main(string[] args)
        {
            // 创建学生对象
            Student student1 = new Student(1001, "张三");
            student1.AddGrade("大一上", 85);
            student1.AddGrade("大一下", 92);
            student1.AddGrade("大二上", 88);

            Student student2 = new Student(1002, "李四");
            student2.AddGrade("大一上", 76);
            student2.AddGrade("大一下", 80);
            student2.AddGrade("大二上", 90);

            // 学生1选择按学期打印模板
            student1.GP = GradeReport.OrderByTerm;
            Console.WriteLine("学生1使用按学期打印模板：");
            student1.PrintGrade();

            // 学生1切换为按成绩排序模板
            student1.GP = GradeReport.OrderByScore;
            Console.WriteLine("学生1使用按成绩高低打印模板：");
            student1.PrintGrade();

            // 学生2选择简洁打印模板
            student2.GP = GradeReport.SimpleFormat;
            Console.WriteLine("学生2使用简洁打印模板：");
            student2.PrintGrade();

            // 演示动态切换模板
            student2.GP = GradeReport.OrderByTerm;
            Console.WriteLine("学生2切换为按学期打印模板：");
            student2.PrintGrade();
        }
    }
}
